package com.fastadmin.config.quartz;

import cn.hutool.core.thread.ThreadUtil;
import lombok.extern.slf4j.Slf4j;
import org.quartz.JobExecutionContext;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;

@Slf4j
public class ExecutionJob extends QuartzJobBean {

    /**
     * 该处仅供参考
     */
    private final static ThreadPoolExecutor EXECUTOR = ThreadUtil
            .newExecutor(20, 200);

    @Override
    public void executeInternal(JobExecutionContext context) {
        QuartzJob quartzJob = (QuartzJob) context.getMergedJobDataMap().get(QuartzJob.JOB_KEY);
        // 获取spring bean
        long startTime = System.currentTimeMillis();
        try {
            // 执行任务
            log.info("任务开始执行，任务名称：" + quartzJob.getJob_name());
            QuartzRunnable task = new QuartzRunnable(quartzJob.getBean_name(), quartzJob.getMethod_name(),
                    quartzJob.getParams());
            Future<?> future = EXECUTOR.submit(task);
            future.get();
            long times = System.currentTimeMillis() - startTime;
            // 任务状态
            log.info("任务执行完毕，任务名称：" + quartzJob.getJob_name() + ", 执行时间：" + times + "毫秒");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
        }
    }

}
